/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : ad7175_ctrl.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/

#include "stm32f4xx_hal.h"
#include "main.h"
#include "da5541_ctrl.h"
#include "Parameter.h"

//extern ParameterHandle_Typedef g_tgParameterSave;

extern SPI_HandleTypeDef hspi2;


/******************************************************************************
	* @说明  	DA5541写输出电压
						
  * @参数  
						vol   DA5541输出的电压
						 
  * @注意  	无
  
  * @返回值 无
 ******************************************************************************/
 uint16_t abc;
void DA5541_Volset(uint16_t vol)
{
	if(vol==0x0000)
	{
		vol=0x0001;
	}
	if(vol>=0xFFFF)
	{
		vol=0xFFFF;
	}
	abc=vol;
	uint8_t buff[2]={vol>>8&0xff,vol&0xff};
	
	HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_RESET);
//	HAL_Delay(5);
	HAL_SPI_Transmit(&hspi2, &buff[0], 1, 100);
	HAL_SPI_Transmit(&hspi2, &buff[1], 1, 100);
	
	HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET);

}
/******************************************************************************
	* @说明  	设置DA0输出电压
						
  * @参数  
						vol   DA0输出的电压
						 
  * @注意  	无
  
  * @返回值 无
 ******************************************************************************/

void DA5541_Generate_CS0(float vol)
{
	uint16_t Vol_Change;
	
//	vol = vol-Kda_Out;
  vol = vol-Kda_Out;//负值有少许偏置
	vol=vol>1.0f?1.0f:vol;
	vol=vol<-1.0?-1.0f:vol;
	vol=(vol+2.5f)/2;
	Vol_Change = (vol/2.5f)*65536;		//	Vol_Change = (vol/2.480+(2.480-vol)*0.00222)*65536;		//vol/5.0*32768+32768;	vol/2.489*65536;	+(2.48-vol)*0.0015;	(vol/2.480+(2.480-vol)*0.002)*65536;
	DA5541_Volset(Vol_Change);

}
/******************************************************************************
	* @说明  DA5541初始化
						
  * @参数  
						无
						 
  * @注意  	无
  
  * @返回值 无
 ******************************************************************************/
void DA5541_Init(void)
{
	DA5541_Generate_CS0(0);
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


